import React from 'react'
// ===================================================================== antd
import { WarningFilled } from '@ant-design/icons'
// ===================================================================== Temp
import TableModal from '#cpt/temp/TableModal'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== Lazy
const message 			= $Lazy.file(()=>import('@antd/message'))
// =====================================================================
const sexData = [ { name: "男", value: "45701" },{ name: "女", value: "45702" }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data: [],
		formData : [
			{ label: '用户名',		name: 'phone',		half:true,	disabled:true,	p:' ' },
			{ label: '密码',		name: 'password',	half:true,	type:'password',	required:true },
		],
	}
	
	model = {}
	flag = 0
	datas = []
	user = {}
	open = data => {
		this.datas = data
		this.modal.open({formRef:this.formRef})
		this.setState({ data:[data[0]]})
		this.flag = 0
		this.user = $fn.getUser()
		this.formRef.setValue({'phone': this.user.empl_name,'password':''})
	}
	
	// 表单
	cols = [
		{ title: '条码号',			dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '科室', 			dataIndex: 'dept_name', 		width:180 },
		// { title: '岗位', 			dataIndex: 'project_name', 		width:180 },
		{ title: '项目', 			dataIndex: 'kind_name', 		width:180 },
		{ title: '自然项目', 		dataIndex: 'parent_kind_name', 	width:180 },
		{ title: '危急值', 			dataIndex: 'crisis_value', 		width:100 },
		{ title: '姓名', 			dataIndex: 'name', 				...$fn.table.name },
		{ title: '性别', 			dataIndex: 'sex', 				...$fn.table.sex,		render: text => $fn.getState(sexData,text) },
		{ title: '结果描述', 		dataIndex: 'result_des', 		width:180 },
		{ title: '送检医院', 		dataIndex: 'hosp_name', 		width:180 },
		{ title: '送检医院科室', 	dataIndex: 'depart', 			width:180 },
		{ title: '紧急联系人', 		dataIndex: 'crisis_linkman', 	...$fn.table.name,		render: text => $fn.isEmpty(text)?'无':text },
		{ title: '紧急联系电话', 	dataIndex: 'crisis_phone', 		...$fn.table.phone,		render: text => $fn.isEmpty(text)?'无':text },
		{ title: '创建时间', 		dataIndex: 'created_at', 		...$fn.table.time }
	]
	
	confirmOk = () => {
		const pwd = this.formRef.getValue(['password'])
		if($fn.isEmpty(pwd.password)){
			message.then(f=>f.error('请输入密码'))
		}else{
			const param = { user_id:this.user.uuid, ...pwd, uuid:this.state.data[0].uuid }
			$http.submit(this,'ts-crisis/edit',{param:param }).then((data)=>{
				if($fn.hasObject(data)){
					if(+data.data === 1){
						message.then(f=>f.success('已确认'))
						this.flag += 1
						if(this.flag < this.datas.length){
							this.setState({data:[this.datas[this.flag]]})
						}else{
							message.then(f=>f.success('您已全部确认完成'))
							setTimeout(()=> this.modal.close(), 500);
						}
						this.formRef.reset(['password'])
					}else{
						message.then(f=>f.error(data?.msg))
					}
				}
			})
		}
	}
	
	render(){
		const { datas, flag } = this
		return (
			<>
				{TableModal.call(this,{ title:'危急值处理', width:'80%', height:'60%', noCancel:true, closable:false, 
					onOk:()=>{this.confirmOk()}, okText:`确认${flag+1}/${datas.length}`,scroll:true,
					onInit: this.props.onInit,
					prefix: <div className='tc mb5 f14' style={{color:'red'}} ><WarningFilled className='plr5' /> 请注意，出现危急值，需尽快确认结果，报告临床！</div>,
				})}
			</>
		)
	}
}

export default Index